﻿using InstallWizard.Model;
using MyCommon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InstallWizard
{
    public partial class Page3 : Form
    {
        AppInfo appInfo = null;
        string configFile = "wizard.config";
        public Page3()
        {
            InitializeComponent();
          
        }
        #region 单独不现实窗体关闭按钮
        private const int CP_NOCLOSE_BUTTON = 0x200;
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams myCp = base.CreateParams;
                myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
                return myCp;
            }
        }
        #endregion
        private void Page3_Load(object sender, EventArgs e)
        {
            
        }
        private void Page3_Shown(object sender, EventArgs e)
        {
            appInfo = DAL.FileIO.ReadConfigFile(configFile, out List<AppFileInfo> appFileList);
            var persernalPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            Text = appInfo.GetAppNameVer() + "安装向导";
            txtTargetFolder.Text = persernalPath + "\\" + appInfo.CompanyName;
            txtWorkingFolder.Text = txtTargetFolder.Text;
            txtProgramGroup.Text = appInfo.CompanyName;
            ShowSpace(appInfo.Size_MB, "c:\\");
        }
        private void BtnTargetFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog
            {
                SelectedPath = txtTargetFolder.Text,
                ShowNewFolderButton = true
            };
            if(fd.ShowDialog()==DialogResult.OK)
            {
                if(Directory.GetFiles(fd.SelectedPath)!=null)
                {
                    if (MessageBox.Show("如果继续，可能会覆盖目标目录的内容，请确认目标目录是否正确！", "目标目录不是空的",
                        MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.Cancel)
                        return;
                }
                txtTargetFolder.Text = fd.SelectedPath;
                txtWorkingFolder.Text = fd.SelectedPath;
                ShowSpace(20.4f, fd.SelectedPath);
            }
        }

        private void ShowSpace(float neededMB, string path)
        {
            float space = DiskHelper.GetDiskPartitionFreeSpace(path);
            space /= 1024 * 1024;
            lbSpace.Text = $"需要空间： {neededMB}MB, 可用空间{space}MB";
        }
        private void BtnWorkingFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fd = new FolderBrowserDialog
            {
                SelectedPath = txtWorkingFolder.Text,
                ShowNewFolderButton = true
            };
            if (fd.ShowDialog() == DialogResult.OK)
            {                
                txtWorkingFolder.Text = fd.SelectedPath;
            }
        }

        private void BtnPrev_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form[] pages = Tag as Form[];
            pages[1].Show();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            InstallOptions options = new InstallOptions
            {
                TargetFolder = txtTargetFolder.Text,
                WorkingFolder = txtWorkingFolder.Text,
                Install4AllUser = rbAllUser.Checked,
                ProgramGroup = txtProgramGroup.Text,
                DesktopShortcut = chkShortcutOnDesktop.Checked
            };

            this.Hide();
            Form[] pages = Tag as Form[];

            ((Page4)pages[3]).LoadInstallOptions(options);//把安装选项传递到第四页
            pages[3].Show();
        }

       
        private void Page3_FormClosed(object sender, FormClosedEventArgs e)
        {
            //Application.Exit();
        }

        private void TxtTargetFolder_Validated(object sender, EventArgs e)
        {
            
        }

        private void RbAllUser_CheckedChanged(object sender, EventArgs e)
        {
            var commPath  = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            var persernalPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            if(rbAllUser.Checked)
            {
                txtTargetFolder.Text = commPath + "\\" + appInfo.CompanyName;
            }
            else
            {
                txtTargetFolder.Text = persernalPath + "\\" + appInfo.CompanyName;
            }

        }

        private void RbCurrendUser_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void TxtTargetFolder_TextChanged(object sender, EventArgs e)
        {
            txtWorkingFolder.Text = txtTargetFolder.Text;
        }

       
    }
}
